package com.zdy.im.user.dao;


import java.util.Map;

import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.core.mybatis.MyBatisBaseCacheDao;
import com.sixeco.framework.util.PageUtils;
import com.zdy.im.user.dto.request.ImMessageReadRequestDto;
import com.zdy.im.user.entity.ImMessageRead;
import com.zdy.im.user.mapper.ImMessageReadMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;




@Service("imMessageReadDao")
public class ImMessageReadDao extends MyBatisBaseCacheDao<ImMessageReadMapper, ImMessageRead> {

    @Autowired
    private ImMessageReadMapper  imMessageReadMapper;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImMessageRead>  iPage =  new Query<ImMessageRead>().getPage(params);

        QueryWrapper<ImMessageRead> query =  new QueryWrapper<>();

        IPage<ImMessageRead> page = this.page(iPage,query );

        return new PageUtils(page);
    }


    @Override
    public void saveData(List<Object> list) {
        List<ImMessageRead> datas = new ArrayList<>();
        list.forEach(e -> datas.add(ImMessageRead.build().clone(e)));
        this.saveBatch(datas, BATCH_SIZE);
        }

    public ImMessageRead getImMessageReadByUser(ImMessageReadRequestDto im) {
        QueryWrapper<ImMessageRead> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", im.getUserId()).eq("type",im.getType())
                .eq("message_id", im.getMessageId());
        List<ImMessageRead> imMessageReads = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imMessageReads)){
            return null;
        }
        return imMessageReads.get(0);
    }

    public List<ImMessageRead> getImMessageReadMapByUserId(long userId) {
        QueryWrapper<ImMessageRead> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<ImMessageRead> imMessageReads = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imMessageReads)){
            return null;
        }
        return imMessageReads;
    }
}